<?php

class DeliveryController extends Controller {
	
    public function __construct($data) {
        parent::__construct($data);

    }

    public function refer() {
       require_once('../system/ecjp_m.php');
      //ヘッド、メニューなどsmarty設定
	   $this->_set_head_footer();
	   
	   $this->_load_mdao_class("t_m_delivery");
	   $dao = new t_m_delivery;
	   
       $forms = $this->g;
    
   
       $wherearr = array();
        if (isset($forms['id']) && (!empty($forms['id']) )) {
            $wherearr[] = sprintf("login_id='%s'",$forms['id']);
        }
	   	   
	   if(isset($forms['page']) && is_numeric($forms['page'])) {
            $dosearch = TRUE;
            $page['current'] = $forms['page'];
        } else {
            $page['current'] = 1;
        }
        $page['item'] = PAGE_MAX;
         //print_r($wherearr);		
        $data = $dao->search($wherearr, $page);
	  
         if (isset($page))
            $this->assign('page', $page);
        
         if (isset($data))
            $this->assign('data', $data);
        
        	$this->assign('forms', $forms);
            $this->assign('pageing', $this->fetch('page.html'));
            $this->display('user_delivery.html');
    }
    
   public function form(){
   	$this->_load_Class_DAO_OTHER();
   	$dao_local = Class_DAO_OTHER::get_local_array('都道府県');
   	if (isset($dao_local))
   		$this->assign('dao_local', $dao_local);
   	$show_massg=true;
   	$show_massg2=true;
   	$forms=$this->g;
   	$this->assign('show_massg',$show_massg);
   	$this->assign('show_massg2',$show_massg2);
   	$this->assign('forms',$forms);
   	$this->_set_head_footer();
   	$this->display('delivery_form.html');
   }
    
    
   public function update(){
   	require_once('../system/ecjp_m.php');
   	$this->_load_php_file("Class_ERROR.php");
   	// require_once( '../system/Class_FUNC.php' );
   	$this->_load_php_file("Class_FUNC.php");
   	$this->_load_mdao_class("t_m_delivery");
   	$this->_load_Class_DAO_OTHER();
   	$show_massg=true;
	$dao = new t_m_delivery;
	
	$forms=$this->g;
	if(!$forms){$forms=$this->p;}
	if($forms['sent_id']){
	$sql='select * from  d_delivery where sent_id='.$forms['sent_id'];
		$row=$dao->get_rows($sql);
		}

	   if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
	   	$forms = $this->p;
	   	$show_massg2=true;
	 
	   	$err     = new Class_ERROR;
	   	$chk     = $dao->get_checks();
	   	
	   	$err_mes['login_id'] = $err->exist_check($forms['id']);
	   	$err_mes['sent_name'] = $err->exist_check($forms['sent_name']);
	   	$err_mes['sent_name1'] = $err->exist_check($forms['sent_name1']);
	   	if(!$err_mes['sent_name1'])
	   	$err_mes['sent_name1'] = $err->exist_check($forms['sent_name2']);
	   	//$err_mes['sent_workshop_name'] = $err->exist_check($forms['sent_workshop_name']);
	   //	$err_mes['sent_name_furikana1'] = $err->exist_check($forms['sent_name_furikana1']);
	   //	$err_mes['sent_name_furikana2'] = $err->exist_check($forms['sent_name_furikana2']);
	   //	$err_mes['sent_zipcode'] = $err->exist_check($forms['sent_zipcode']);
	   //	$err_mes['sent_local_cd'] = $err->exist_check($forms['sent_local_cd']);
	   	$err_mes['sent_treet_name'] = $err->exist_check($forms['sent_treet_name']);
	   //	$err_mes['sent_address'] = $err->exist_check($forms['sent_address']);
	   	$err_mes['sent_tel_number1'] = $err->exist_check($forms['sent_tel_number1']);
	   	if(!$err_mes['sent_tel_number1'] )
	   	$err_mes['sent_tel_number1'] = $err->exist_check($forms['sent_tel_number2']);
	   	
	   	
	  //print_r($err_mes);
	   	if ($err->clear) {
	   		
	   		$err_mes['sent_name']=$err->platform_dependent_characters_filter($forms['sent_name']);
	   		$err_mes['sent_workshop_name']=$err->platform_dependent_characters_filter($forms['sent_workshop_name']);
	   		//$err_mes['sent_zipcode'] = $err->zipcode_check($forms['sent_zipcode1'] . '-' . $forms['sent_zipcode2']);
	   		$err_mes['sent_local_cd'] = $err->number_check($forms['sent_local_cd']);
	   		$err_mes['sent_treet_name']=$err->platform_dependent_characters_filter($forms['sent_treet_name']);
	   		//$err_mes['sent_address']=$err->platform_dependent_characters_filter($forms['sent_address']);
	   		$err_mes['sent_tel_number'] = $err->tel_check($forms['sent_tel_number1'] . '-' . $forms['sent_tel_number2'] . '-' . $forms['sent_tel_number3']);
	   		$err_mes['login_id']   = $err->exist_check($forms['id']);
	   		$err_mes['sent_id'] = $err->number_check($forms['sent_id']);
	   		
	   	}
	 
	  	
	 	//print_r($forms);
	   	
	   	
	   	
	  
	   	
	   	if ($err->clear){
	   		//$values["c_time"] = date('Y-m-d');
	   		//$values["u_time"] = date('Y-m-d');
	   		$values["sent_name"] = $forms['sent_name'];
	   		$values["sent_name1"] = $forms['sent_name1'];
	   		$values["sent_name2"] = $forms['sent_name2'];
	   		$values["sent_workshop_name1"] = $forms['sent_workshop_name'];
	   		$values["sent_workshop_name2"] = $forms['sent_workshop_name'];
	   		$values["sent_local_cd"] = $forms['sent_local_cd'];
	   		$values["sent_treet_name"] = $forms['sent_treet_name'];
	   		$values["sent_zipcode"] = $forms['sent_zipcode1']."-".$forms['sent_zipcode2'];
	   		$values["sent_address"] = $forms['sent_address'];
	   		$values["sent_tel_number"] = $forms['sent_tel_number1']."-".$forms['sent_tel_number2']."-".$forms['sent_tel_number3'];
	   		$values["login_id"] = $forms['id'];
	   		
	   		
	   		if($row){
	   			
	   			if($dao->edit($row['0']['sent_id'], $values)){
	   				//echo "<script>alert('更新成功!');</script>";
	   				$this->assign('message', '更新が完了しました。');
	   				$show_massg=false;
	   			}else{
	   				$this->assign('message', '更新が失敗しました。');
	   				//echo "<script>alert('更新失败!');</script>";
	   				$show_massg=false;
	   				}
	   			}else{
	   				
	   				if($dao->add0($values)){
	   					$this->assign('message', '登録が完了しました。');
	   					//echo "<script>alert('插入成功!');</script>";
	   					$show_massg=false;
	   				}else{
	   				$this->assign('message', '登録が失敗しました。');
	   					//echo "<script>alert('插入失败!');</script>";
	   				$show_massg=false;
	   				}	
	   			}
	   			
	   			
	   	}
	   	
	   	
	   	
	   	
	   }
	   
	 if (isset($row)){
	 	$strs = explode('-',$row[0]['sent_tel_number']);
	 	$strs2 = explode('-',$row[0]['sent_zipcode']);
	 	$row[0]['sent_zipcode1']=$strs2[0];
	 	$row[0]['sent_zipcode2']=$strs2[1];
	 	
	 	$row[0]['id']=$row[0]['login_id'];
	 	$row[0]['sent_tel_number1']=$strs[0];
	 	$row[0]['sent_tel_number2']=$strs[1];
	 	$row[0]['sent_tel_number3']=$strs[2];
	 	$row[0]['sent_workshop_name']=$row[0]['sent_tel_number1'];
	 	
	 	
	 	
	 	$this->assign('forms', $row[0]);
	 	}elseif(isset($forms)){
	 	$this->assign('forms', $forms);
	 }
	 
	$dao_local = Class_DAO_OTHER::get_local_array('都道府県');
	 if (isset($dao_local))
	 	$this->assign('dao_local', $dao_local);
	 $this->assign('err_mes', $err_mes);
	 $this->assign('show_massg2',$show_massg2);
	 $this->assign('show_massg',$show_massg);
   	$this->_set_head_footer();
   	$this->display('delivery_form.html');
   }
    
    
    
}